<?php
declare(strict_types=1);

namespace App\AdminApi\Api;

use App\AdminApi\AdminApiBasicsController;
use App\AdminApi\Module\Admin\Service\MenuService;
use App\Sys;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Psr\Http\Message\ResponseInterface;
use Zms\AdminHelper\TheServer;
use Zms\Attribute\Describe;
use Zms\Guard\Auth;
use zms\HyperfResponse\Response;
use Zms\Service\System;


#[
    Controller(prefix: '/admin-api/basics', server: self::SERVER),
    Auth(guard: self::GUARD, app: self::APP)
]
class BasicsController extends AdminApiBasicsController
{
    #[
        Describe('获取后台配置'),
        GetMapping('configure')
    ]
    public function configure(): ResponseInterface
    {

        $configure = Sys::conf('admin'); //后台配置

        $configure['debug'] = Sys::develop();//是否开发模式

        $configure['map'] = Sys::conf('map');//腾讯地图

        $configure['ai'] = Sys::conf('ai.open/int') === 1;//腾讯地图

        $configure['company'] = Sys::conf('system.company');// 公司名称

        $configure['environment'] = System::environment();//运行环境

        $configure['host'] = Sys::conf('system.host');//域名

        $configure['mourn'] = Sys::conf('system.mourn/int');//哀悼模式

        return Response::Api()->success(data: $configure);
    }


    #[
        GetMapping('login-refresh'),
        Auth(label: ['登录刷新'], sign: "skip", logger: false)
    ]
    public function LoginRefresh(): ResponseInterface
    {
        if ($this->guard()->tourist()) {
            return Response::Api()->fail('请先登录');
        }
        $user = $this->user();
        $userData = $user->userInfo();
        return Response::Api()->success(data: [
            'menu' => Sys::container(MenuService::class)->getAdministerMenu($user),
            'user' => $userData
        ]);
    }


    #[
        GetMapping('system-info'),
    ]
    public function SystemInfo(TheServer $server): ResponseInterface
    {
        $composer = $server->composerInfo();
        $os = System::environment();
        $result['program'] = [
            'os' => "[{$os['type']}] {$os['name']}",
            'swoole_version' => SWOOLE_VERSION,
            'php_version' => PHP_VERSION,
            'hyperf_version' => $composer['hyperf/framework'] ?? '?',
            'mysql_version' => Db::selectOne('SELECT @@version as `version`')->version ?? '',
        ];
        $result['run'] = time() - RUN_TIME;
        return Response::Api()->success(data: $result);
    }

    #[
        GetMapping('logger-size')
    ]
    public function loggerSize(TheServer $server): ResponseInterface
    {
        $size = $server->loggerSize();
        return Response::Api()->success(data: $size);
    }
}